package helper

import (
	"fmt"
	"gitee.com/jjawesomejj/awesome-util/helper/commonhelper"
	"regexp"
	"strings"
)

const ENV_REG = "\\{\\{\\{(.*?)=([\\s\\S]*?)\\}\\}\\}"
const PARAMS_REG = "--(.*?)=(.*?)$"

func EchoEnv(envKey string, envValue string) {
	fmt.Println(commonhelper.Format("<<{}={}>>", envKey, envValue))
}
func GetRunningEnvParams(content string) map[string]string {
	compile, err := regexp.Compile(ENV_REG)
	if err != nil {
		return map[string]string{}
	}
	result := map[string]string{}
	matchs := compile.FindAllStringSubmatch(content, -1)
	for _, match := range matchs {
		if len(match) == 3 {
			result[strings.Trim(match[1], " ")] = strings.Trim(match[2], " ")
		}
	}
	return result
}

func GetCommandParamsFormStr(content string) map[string]string {
	compile, err := regexp.Compile(PARAMS_REG)
	if err != nil {
		return map[string]string{}
	}
	result := map[string]string{}
	matchs := compile.FindAllStringSubmatch(content, -1)
	for _, match := range matchs {
		if len(match) == 3 {
			result[strings.Trim(match[1], " ")] = strings.Trim(match[2], " ")
		}
	}
	return result
}
